package com.meiyou.service.impl;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import com.meiyou.mapper.AddProjectMapper;
import com.meiyou.service.AddProjectService;

@Service
public class AddProjectServiceImpl implements AddProjectService{
	
	@Autowired
	AddProjectMapper addproject;
	String appointmentimg;
    
	/**
	 * 发布同城约会项目
	 */

	public boolean insAppointment(String jindu, String weidu, String context, String date, String starttime,
			String endtime, int num, int meijin, boolean ispingtai, String ucount, CommonsMultipartFile img,
			String address,HttpServletRequest req) {
		// TODO Auto-generated method stub
		String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		String fatime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		if(saveImg(img,req.getSession().getServletContext().getRealPath("/imgs/appointment/")+time+img.getOriginalFilename())) {  //保存约会项目上传的图片
			appointmentimg = "imgs/appointment/"+time+img.getOriginalFilename();
			if(addproject.addAppointment(jindu, weidu, context, date, starttime, endtime, num, meijin, ispingtai, ucount, appointmentimg, address, fatime)==1) {
				return true;
			}
			return false;
		}
		else {
			return false;
		}
	}

	/**
	 * 保存项目图片
	 * @param file
	 * @param path
	 * @return
	 */
	public boolean saveImg(CommonsMultipartFile file,String path) {
//		String path = request.getSession().getServletContext().getRealPath("/imgs/appointment/")+file.getOriginalFilename();//上传的目标路径和文件
		System.out.println("上传路径为"+path);
		File newfile = new File(path);
		try {
			file.transferTo(newfile);  //上传文件
			return true;
			
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;			
		} catch (IOException e) {
			// TODO Auto-generated catch block	
			e.printStackTrace();
			return false;
		}
	}
}
